<%--
  Created by IntelliJ IDEA.
  User: 92460
  Date: 2020/12/18
  Time: 15:45
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>注册页面</title>
</head>
<script src="${pageContext.request.contextPath}/js/jquery-3.5.1.js"></script>
<body>
<%
    String path = request.getContextPath() + "/servlet/DoReg";
%>

<p>用户名：<input type="text" name="username" onblur="checkUser()"><span id="msg"></span></p>


<script>
    let xmlHttp = new XMLHttpRequest();
    let responseText = xmlHttp.responseText;

    function checkUser() {
        let username = $("input[name='username']").val();
        if (!username) {
            $("#msg").text("用户名不能为空");
        } else {
            let url = "${pageContext.request.contextPath}" + "/servlet/doReg?username=" + username;
            $.ajax({
                "url": url,
                "type": "post",
                "data": "username=" + username,
                "dataType": "text",
                "success": doResponse,
                "error": function () {
                    alert("服务器未响应，请稍后再试");
                }
            });

        }
    }

    function doResponse(data) {
        if (data == 1) {
            // 重名了
            $("#msg").text("用户名已存在！");
        } else {
            $("#msg").text("用户名可以使用！");
        }

    }


</script>
</body>
</html>
